import {type Timestamp, TS} from "./Timestamp.ts";

export interface EmoteGroup {
    name: string,
    emotes: Emote[]
}

export interface BbsCodeBody {
    fontColor: string[]
    emoteGroups: EmoteGroup[]
}

export interface Emote {
    groupKey: string,
    name: string,
    code: string,
    url: string
}

export const buildEmoteGroup = (data: any) => Object.keys(data).map((key: string) => {
    const group = data[key]
    const nameKey = '_______name'

    return {
        name: group[nameKey],
        emotes: Object.keys(group).filter(k => k !== nameKey).map((k: string) => {
            const filename = group[k]
            return {
                groupKey: key,
                name: k,
                code: `[s:${key === '0' ? '' : key}:${k}]`,
                url: `https://img4.nga.178.com/ngabbs/post/smile/${filename}`
            }
        })
    } as EmoteGroup
})

export interface IndexForum {
    forumId?: number | undefined,
    name?: string | undefined,
    simpleName?: string | undefined,
    info?: string | undefined,
    simpleInfo?: string | undefined,
    longInfo?: string | undefined,
    bit?: number | undefined,
    colTid?: number | undefined,
}

export interface IndexForumGroup {
    name: string | undefined
    forums: IndexForum[]
}

export interface IndexForumCategory {
    name: string,
    nameEn: string,
    groups: IndexForumGroup[]
}

export interface IndexForumBody {
    // 更新时间
    time: Timestamp | undefined
    categories: IndexForumCategory[]
}

export const buildIndexForum = (data: any): IndexForum => {
    return {
        forumId: Number(data.fid),
        name: data.name,
        simpleName: data.nameS,
        info: data.info,
        simpleInfo: data.infoS,
        longInfo: data.infoL,
        bit: Number(data.bit),
        colTid: Number(data.stid),
    }
}

export const buildIndexForumGroup = (data: any): IndexForumGroup => {
    return {
        name: data.name,
        forums: Object.values(data.content).map(buildIndexForum),
    }
}

export const buildIndexForumCategory = (data: any): IndexForumCategory => {
    return {
        name: data.name,
        nameEn: data.id,
        groups: Object.values(data.content).map(buildIndexForumGroup).filter(i => i.forums.length > 0),
    }
}

export const buildIndexForumBody = (body: any): IndexForumBody => {
    const data = body?.data?.[0]?.all
    return {
        time: TS(body.time),
        categories: Object.values(data).map(buildIndexForumCategory).filter(i => i.groups.length > 0),
    }
}

// 含有变身效果的buff
export interface AvatarBuff {
    id: number
    filename: string
    suffix?: string | undefined
    url: string
}

export interface UiResource {
    avatarBuffs: AvatarBuff[]
}

export const buildUiResource = (text: string): UiResource => {
    const prefix = 'https://img4.nga.178.com/ngabbs/face/'
    const jsonText = /__AVATAR_LIST =([\s\S]+?)\n\n/.exec(text)?.[1]
        ?.replace(/(\d+) :/g, "\"$1\":")
        ?.replace(/url:/g, "\"url\":")
        ?.replace(/append:/g, "\"append\":")
        ?.replace(/func:/g, "\"func\":")
        ?.replace(/'/g, "\"")

    const json = jsonText && JSON.parse(jsonText)

    const avatarBuffs = json && Object.keys(json).map((key: string) => {
        const value = json[key]
        if (typeof value === 'string') {
            return {
                id: Number(key),
                filename: value,
                url: prefix + value,
            }
        } else {
            return {
                id: Number(key),
                filename: value.url,
                suffix: value.append,
                url: prefix + value.url,
            }
        }
    })
    return {avatarBuffs}
}